package com.yourcompany.service.factory;

import com.yourcompany.api.strategy.PaymentStrategy;
import com.yourcompany.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

// 支付策略工厂
@Component
public class PaymentStrategyFactory {
    
    private final Map<String, PaymentStrategy> strategyMap;
    
    @Autowired
    public PaymentStrategyFactory(List<PaymentStrategy> strategies) {
        strategyMap = strategies.stream()
                .collect(Collectors.toMap(PaymentStrategy::getPaymentMethod, Function.identity()));
    }
    
    public PaymentStrategy getStrategy(String paymentMethod) {
        PaymentStrategy strategy = strategyMap.get(paymentMethod);
        if (strategy == null) {
            throw new BusinessException("不支持的支付方式");
        }
        return strategy;
    }
} 